package com.sss.otep.client.proxy;

import java.io.File;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.ws.client.core.WebServiceTemplate;

import com.sss.otep.client.grid.GridDataModel;
import com.sss.otep.client.grid.MemberDataModel;
import com.sss.otep.client.grid.MemberPaidDeptDataModel;
import com.sss.otep.message.schemas.MemberPaidDeptRequest;
import com.sss.otep.message.schemas.MemberPaidDeptResponse;
import com.sss.otep.message.schemas.MemberRequest;
import com.sss.otep.message.schemas.MemberResponse;
import com.sss.otep.message.schemas.ObjectFactory;
import com.sss.otep.service.LoadParameter;
import com.sss.otep.util.ClassPathResourceUtil;
import com.sss.otep.util.SchemaConvertionUtils;

@Service
public class OTEPWebServiceProxy extends AbstractSSLClient implements
		OTEPClient {


	
	private static final Logger log = LoggerFactory
			.getLogger(OTEPWebServiceProxy.class);

	@Override
	public GridDataModel<MemberDataModel> loadMember(LoadParameter param)
			throws Exception {

		MemberRequest request = getObjectFactory().createMemberRequest();

		if (param.getId() != null) {
			request.setId(param.getId());
		}

		request.setIdCard(param.getIdCard());
		request.setName(param.getName());
		request.setSurname(param.getSurname());
		GridDataModel<MemberDataModel> model = new GridDataModel<MemberDataModel>();
		try {
//			MemberResponse response = (MemberResponse) getSSLWebServiceTemplate()
//					.marshalSendAndReceive(request);

			MemberResponse response = (MemberResponse) getWebServiceTemplate()
			.marshalSendAndReceive(request);
			log.info("loadMember result size :: {} ", response.getMember().size());
			for (com.sss.otep.message.schemas.Member m : response.getMember()) {
				model.addData(SchemaConvertionUtils.toGridModel(model
						.getTotal(), m));
			}
		} catch (Exception e) {
			throw e;
		}

		return model;
	}

	@Override
	public GridDataModel<MemberPaidDeptDataModel> loadMemberPaidDept(
			LoadParameter param) throws Exception {
		MemberPaidDeptRequest request = getObjectFactory()
				.createMemberPaidDeptRequest();

		if (param.getId() != null) {
			request.setId(param.getId());
		}

		request.setIdCard(param.getIdCard());
		request.setName(param.getName());
		request.setSurname(param.getSurname());
		GridDataModel<MemberPaidDeptDataModel> model = new GridDataModel<MemberPaidDeptDataModel>();
		try {
//			MemberPaidDeptResponse response = (MemberPaidDeptResponse) getSSLWebServiceTemplate()
//					.marshalSendAndReceive(request);
			
			MemberPaidDeptResponse response = (MemberPaidDeptResponse) getWebServiceTemplate()
			.marshalSendAndReceive(request);			
			log.info("load member paid dept result size :: {}", response
					.getMemberPaidDept().size());
			for (com.sss.otep.message.schemas.MemberPaidDept m : response
					.getMemberPaidDept()) {
				model.addData(SchemaConvertionUtils.toGridModel(model
						.getTotal(), m));
			}
		} catch (Exception e) {
			throw e;
		}

		return model;
	}

	/**
	 * @return
	 */
	private ObjectFactory getObjectFactory() {
		ObjectFactory factory = new ObjectFactory();
		return factory;
	}

	public WebServiceTemplate getSSLWebServiceTemplate() {
		return super.performSSLWebServiceTemplate();
	}

	public WebServiceTemplate getWebServiceTemplate() {
		return performWebServiceTemplate();
	}

	@Override
	public File getKeyStoreFile() {
		try {
			return ClassPathResourceUtil.getFileFromResource("classpath:keystore/transport/ssl/client.jks");
		} catch (IOException e) {
			log.error("[ Keystore File exception ]", e.getMessage());
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public String getKeyStorePass() {
		return "clientstore";
	}

	@Override
	public File getTrustStoreFile() {
		try {
			return ClassPathResourceUtil.getFileFromResource("classpath:keystore/transport/ssl/truststore.jks");
		} catch (IOException e) {
			log.error("[ TrustStore File exception ]", e.getMessage());
		}
		return null;
	}

	@Override
	public String getTrustStorePass() {
		return "truststore";
	}

}
